package com.ruoyi.project.system.list.controller;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.grouping.domain.Grouping;
import com.ruoyi.project.system.grouping.service.IGroupingService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.list.domain.AccountList;
import com.ruoyi.project.system.list.service.IAccountListService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-04-13
 */
@Controller
@RequestMapping("/system/list")
public class AccountListController extends BaseController
{
    private String prefix = "system/list";

    @Autowired
    private IAccountListService accountListService;
    @Autowired
    private IGroupingService groupingService;

    @Autowired
    private IUserService userService;

//    @RequiresPermissions("system:list:view")
    @GetMapping()
    public String list(Model model)
    {
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        if ("admin".equals(loginUser)){
            List<Grouping> groupings = groupingService.selectGrouping();
            model.addAttribute("groupings",groupings);
        }else {
            List<Grouping> groupings = groupingService.selectByUserName(loginUser);
            model.addAttribute("groupings",groupings);
        }
        model.addAttribute("loginUser",loginUser);
        return prefix + "/list";
    }

    /**
     * 查询【请填写功能名称】列表
     */
//    @RequiresPermissions("system:list:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AccountList accountList)
    {
        startPage();
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        if ("admin".equals(loginUser)){
            List<AccountList> list = accountListService.selectAccountListList(accountList);
            return getDataTable(list);
        }else {
              accountList.setUserName(loginUser);
            List<AccountList> AccountListByUserName = accountListService.selectAccountListByUserName(accountList);
            return getDataTable(AccountListByUserName);
        }
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @RequiresPermissions("system:list:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AccountList accountList)
    {
        List<AccountList> list = accountListService.selectAccountListList(accountList);
        ExcelUtil<AccountList> util = new ExcelUtil<AccountList>(AccountList.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        if ("admin".equals(loginUser)){
            List<Grouping> groupings = groupingService.selectGrouping();
            model.addAttribute("groupings",groupings);
        }else {
            List<Grouping> groupings = groupingService.selectByUserName(loginUser);
            model.addAttribute("groupings",groupings);
        }
//        List<Grouping> groupings = groupingService.selectGrouping();
//        mmap.put("groupings",groupings);
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
//    @RequiresPermissions("system:list:add")
    @Transactional
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AccountList accountList)
    {
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        String account = accountList.getAccount();
        AccountList accountList1 = new AccountList();
        accountList1.setAccount(account);
        List<AccountList> accountLists = accountListService.selectAccountListList(accountList1);
        if (accountLists.size()!=0){
            throw new ServiceException("账号已存在！");
        }
        int number = accountListService.selectNumber(loginUser);
        if (number<=0){
            return AjaxResult.error("添加次数已用完,请购买");
        }
        int i = accountListService.insertAccountList(accountList);
        if (i>0){
            int newNumber=number-1;
            accountListService.updateNumber(newNumber, loginUser);
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 修改【请填写功能名称】
     */
//    @RequiresPermissions("system:list:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AccountList accountList = accountListService.selectAccountListById(id);
        mmap.put("accountList", accountList);
        return prefix + "/edit";
    }


    /**
     * 修改保存【请填写功能名称】
     */
//    @RequiresPermissions("system:list:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AccountList accountList)
    {
        return toAjax(accountListService.updateAccountList(accountList));
    }

    //通过账号修改
    @PostMapping("/updateByAccount")
    @ResponseBody
    public AjaxResult updateByAccount(AccountList accountList){
        return toAjax(accountListService.updateAccountByAccount(accountList));

    }


    //获取账号列表

    @PostMapping("/selectAccount")
    @ResponseBody
    public List<AccountList> selectAccount(Integer groupId){
        List<AccountList> list = accountListService.selectAccount(groupId);
        return list;
    }


    /**
     * 删除【请填写功能名称】
     */
//    @RequiresPermissions("system:list:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    @Transactional
    public AjaxResult remove(String ids)
    {
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        int number = accountListService.selectNumber(loginUser);
        int newNumber=number+1;
        accountListService.updateNumber(newNumber, loginUser);
        return toAjax(accountListService.deleteAccountListByIds(ids));
    }


    //导入数据库
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<AccountList> util = new ExcelUtil<AccountList>(AccountList.class);
        List<AccountList> accountListList = util.importExcel(file.getInputStream());

        for (int i = 0; i < accountListList.size(); i++) {
            AccountList accountList = accountListList.get(i);
            accountListService.insertAccountList(accountList);
        }
        return AjaxResult.success();
    }


    //下载模板
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate(){
        ExcelUtil<AccountList> util=new ExcelUtil<AccountList>(AccountList.class);
        return util.importTemplateExcel("账号信息");
    }
}
